package xiangya.bysj.online;


import java.util.Collections; 
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import javax.annotation.Resource;


import org.springframework.stereotype.Service;

import xiangya.bysj.entity.Doctor;
import xiangya.bysj.mapper.interf.CentMapper;
import xiangya.bysj.mapper.interf.DoctorMapper;
import xiangya.bysj.util.PropertyUtil;


@Service("OnlineDoctorService")
public class OnlineDoctorService {


	
	//积分查询的DAO
	@Resource
	private CentMapper centMapper;
	
	@Resource
	private DoctorMapper doctorMapper;
	private static Random rand=null;
	
	
	
	//小于最小值直接随机返回一名药师
	private int min;
	//百分比第一梯度的最大
	private int begin;
	//百分比第二梯度的最大
	private int step;
	
	
	
	public OnlineDoctorService(){
		rand = new Random();
		
		
		Properties prop = PropertyUtil.loadFile("xiangya-temp.properties");
		
		min=Integer.parseInt((String)prop.get("min"));
		begin=Integer.parseInt((String) prop.get("begin"));
		step=Integer.parseInt((String)prop.get("step"));

	}
	
	
	
	/**
	 * 把目前在线的药师其中一位,按药师积分排序,积分越高越容易被分配到,把积分按begin、 step分为三个梯度 
	 * 返回给用户
	 * @return  药师id 
	 */
	public String get1OnlineDoctor() throws Exception{
		
	
		
		LinkedList<OnlineDoctor> list=Keeper.getKeeper();
	    //if(list.isEmpty()) throw new Exception("没有医师在线");
		if(list.isEmpty()) 
		{
			List<Doctor> dlist=doctorMapper.selectAllDoctor();
			
		    if(dlist.isEmpty()) throw new Exception();

			
			int r=rand.nextInt(Integer.MAX_VALUE);
			
			//如果小于min直接返回
			if(dlist.size()<min){
				return dlist.get(r%dlist.size()).getDoctorCode();
			}
		    
			
		    String result=null;
		    
		    //分为三个梯度0~begin begin~begin+step begin+step~100 
			int level = r%100<begin?1:(r%100<begin+step)?2:3;
			r = rand.nextInt(Integer.MAX_VALUE)%(dlist.size()/3);
			switch(level){
				case 1:
					result=dlist.get(r).getDoctorCode();
				break;
				case 2:
					result=dlist.get((dlist.size()/3)+r).getDoctorCode();
				break; 
				case 3:
					result=dlist.get(((dlist.size()/3)<<1)+r).getDoctorCode();
				break;
			}
			
			return result;
			
		}
		int r=rand.nextInt(Integer.MAX_VALUE);
		
		//如果小于min直接返回
		if(list.size()<min){
			return list.get(r%list.size()).getDoctorid();
		}
	    
		
	    String result=null;
	    
	    //分为三个梯度0~begin begin~begin+step begin+step~100 
		int level = r%100<begin?1:(r%100<begin+step)?2:3;
		r = rand.nextInt(Integer.MAX_VALUE)%(list.size()/3);
		switch(level){
			case 1:
				result=list.get(r).getDoctorid();
			break;
			case 2:
				result=list.get((list.size()/3)+r).getDoctorid();
			break; 
			case 3:
				result=list.get(((list.size()/3)<<1)+r).getDoctorid();
			break;
		}
		
		return result;
		
	}


	
	/**
	 * 当收到来自客户端的确认消息时执行 
	 * 保持药师在线 
	 * @param doctorid 药师id 
	 */ 
	public void keepTouch(String doctorid) throws Exception{
		
		//添加前必须要锁住 linkedlist
		synchronized(Keeper.getKeeper()){
		//如果服务器中不存在 
		boolean has=false;
		
		for(int i=0;i<Keeper.getKeeper().size();i++){
			OnlineDoctor ol = Keeper.getKeeper().get(i);
			if(doctorid.equals(ol.getDoctorid())){
				ol.setCount(0);
				has=true;
			}
		}
		
		
		if(!has){

			Keeper.getKeeper().add(getOnLineDoctor(doctorid));
			}
			//排序以便取出在线积分最高的医师 
			Collections.sort(Keeper.getKeeper());
		}
	
		
	}
	
	
	/**
	 * 返回一个构造好的OnineDoctor对象 
	 * 主要是查询doctor的积分 
	 * @param doctorid
	 * @return
	 * @throws Exception
	 */
	public OnlineDoctor getOnLineDoctor(String doctorid) throws Exception{

		OnlineDoctor ol = new OnlineDoctor();
		
		ol.setCent(centMapper.selectForAnswer(doctorid));
		ol.setDoctorid(doctorid);
		
		return ol;
		
	}
	
	
}
